
package com.example.lin.externalstorage_demo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private ImageView iv_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取外部存储的根目录
        iv_show = findViewById(R.id.iv_show);

    }


    public boolean isExternalStorageWritable() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }

    /* Checks if external storage is available to at least read */
    public boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }

    /**
     * 获取共有的外部存储的路径
     *
     * @param view
     */
    public void test1(View view) {
        Log.d(TAG, "onCreate: " + isExternalStorageWritable());
        if (isExternalStorageWritable()) {
            //获取共有的外部存储的根目录(SD卡的根目录)
            File externalStorageDirectory = Environment.getExternalStorageDirectory();
            Log.i(TAG, "externalStorageDirectory: " + externalStorageDirectory);
            File music = new File(externalStorageDirectory, "Music");
            Log.i(TAG, "music: " + music);

            //获取共有的外部存储的根目录下的系统常用路径(Music,DCIM,Download...)
            File music1 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
            Log.i(TAG, "music1: " + music1);
        }
    }

    /**
     * 获取私有的外部存储路径
     *
     * @param view
     */
    public void test2(View view) {

        if (isExternalStorageWritable()) {
            //获取私有的外部存储的跟目录
            File externalFilesDir = getExternalFilesDir(null);
            //获取私有的外部存储的跟目录下的abc目录
//            File externalFilesDir = getExternalFilesDir("abc");
            Log.d(TAG, "externalFilesDir: " + externalFilesDir.getAbsolutePath());
        }
    }

    public void test3(View view) {
        if (isExternalStorageWritable()) {

            File externalCacheDir = getExternalCacheDir();

            Log.d(TAG, "externalCacheDir: " + externalCacheDir.getAbsolutePath());
        }
    }

    public void loadImage(View view) {
        if (!isExternalStorageWritable())
            return;
        File picDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File file = new File(picDir, "c1.jpg");
//      加载 File 为 Bitmap对象
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

        //将bitmap 绑定到 ImageView上

        iv_show.setImageBitmap(bitmap);
    }
}
